﻿using System;
using System.Collections.Generic;
using System.Text;
using Fasterflect;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.PageControls.PageCommon;

namespace Kugar.UI.Web.AdminLTE.Helpers
{
    public static class TableExtMethod
    {
        public static TableColumn_Index<TData> AddIndex<TData>(this TableColumns<TData> table)
        {
            var column= new TableColumn_Index<TData>(table.Table.ViewContext, table.Table);
            
            table.AddColumn(column);

            return column;
        }

        public static TableColumn_Label<TData> AddLabel<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_Label<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_Textbox<TData> AddTextbox<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_Textbox<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_Checkbox<TData> AddCheckbox<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_Checkbox<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_Radio<TData> AddRadio<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_Radio<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_Selector<TData> AddSelector<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_Selector<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_ButtonList<TData> AddButtonList<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_ButtonList<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_TextboxWithButton<TData> AddTextboxWithButton<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_TextboxWithButton<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_DatetimePicker<TData> AddDatetime<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_DatetimePicker<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_Image<TData> AddImage<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_Image<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }

        public static TableColumn_CustomContent<TData> AddCustomContent<TData>(this TableColumns<TData> table)
        {
            var column = new TableColumn_CustomContent<TData>(table.Table.ViewContext, table.Table);

            table.AddColumn(column);

            return column;
        }
    }

    public static class TableColumnHelperExt
    {
        public static TColumn HideColumn<TColumn>(this TColumn column, bool isHide=true)
            where TColumn : ColumnBase
        {
            column._isHide = isHide;

            return column;
        }

        public static TColumn HeaderCss<TColumn>(this TColumn column, string name)
            where TColumn : ColumnBase
        {
            column._thCss = $"{column._thCss} {name}";

            return column;
        }

        //public static TColumn HeaderStyle<TColumn, TData>(this TColumn column, string name, string value)
        //    where TColumn : ColumnBase<TData>
        //{
        //    column.Attributes.MergeAttr("style", $"{name}:{value};");

        //    return column;
        //}

        public static TColumn CellCss<TColumn>(this TColumn column, string css)
            where TColumn : ColumnBase
        {
           
            column.TrySetFieldValue("_cellCss", css,Flags.InstancePrivate);

            return column;
        }

        //public static TColumn CellStyle<TColumn, TData>(this TColumn column, Func<TData, string> styleFactory)
        //    where TColumn : ColumnBase<TData>
        //{
        //    column._cellStyleFactory = styleFactory;

        //    return column;
        //}

        //public static TColumn Fixed<TColumn, TData>(this TColumn column, ColumnFixedType fixedType)
        //    where TColumn : ColumnBase<TData>
        //{
        //    column._fixed = fixedType;

        //    return column;
        //}

        public static TColumn ColumnTitle<TColumn>(this TColumn column, string title)
            where TColumn : ColumnBase
        {
            column._title = title;

            return column;
        }

        public static TColumn ColumnWidth<TColumn>(this TColumn column, int width) where TColumn : ColumnBase
        {
            column.ColumnMaxWidth(width).ColumnMinWidth(width);

            return column;
        }

        public static TColumn ColumnWidth<TColumn>(this TColumn column, string width) where TColumn : ColumnBase
        {
            column.ColumnMaxWidth(width).ColumnMinWidth(width);

            return column;
        }

        public static TColumn ColumnMinWidth<TColumn>(this TColumn column, int width) where TColumn : ColumnBase
        {
            column._minWidth = $"{width}px";

            return column;
        }

        public static TColumn ColumnMaxWidth<TColumn>(this TColumn column, int width) where TColumn : ColumnBase
        {
            column._maxWidth = $"{width}px";

            return column;
        }

        public static TColumn ColumnMinWidth<TColumn>(this TColumn column, string width) where TColumn : ColumnBase
        {
            column._minWidth = width;

            return column;
        }

        public static TColumn ColumnMaxWidth<TColumn>(this TColumn column, string width) where TColumn : ColumnBase
        {
            column._maxWidth = width;

            return column;
        }

        public static TColumn SortKey<TColumn>(this TColumn column, string sortKey, ColumnSortTypeEnum initSortType= ColumnSortTypeEnum.unsort)
            where TColumn : ColumnBase
        {
            column._sortKey = sortKey;
            column._sortType = initSortType;

            return column;
        }

        public static TColumn HeaderCellAttr<TColumn>(this TColumn column, Action<TagAttrbute> attrConfig) where TColumn : ColumnBase
        {
            var attr = column._headCellAttr ?? new TagAttrbute();

            column._headCellAttr = attr;

            attrConfig(column._headCellAttr);

            return column;
        }
    }

}
